package com.example.zyfyptlw618.avtivivites;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.zyfyptlw618.R;
import com.example.zyfyptlw618.common.Common;
import com.example.zyfyptlw618.iface.CollectListener;
import com.example.zyfyptlw618.iface.FollowListener;
import com.example.zyfyptlw618.model.CollectModel;
import com.example.zyfyptlw618.model.FollowModel;

public class ViewProjectActivity extends AppCompatActivity {
    private WebView webView;
    private int resid;//资源id
    private  int idolid;
    Context context;
    private Boolean flagcollect=false;//收藏标志
    private Boolean flagfollow=false;
    private CollectModel collectmodel;//收藏model
    private FollowModel followModel;
    private SharedPreferences sp;//简单存储
    private String sessionID="";  //sessionid
    private MenuItem item;
    private MenuItem item2;

    CollectListener listener=new CollectListener() {
        @Override
        public void onResponse(String msg) {
            //获取菜单视图
            if (item==null)
                item=findViewById(R.id.menucollect);

            //根据mode中response返回的字符串区分返回结果
            switch (msg)
            {
                case "2": System.out.println("----收藏成功");
                    flagcollect=true;
                    item.setTitle("取消收藏");
                    break;
                case "1":System.out.println("----收藏失败");
                    break;
                case "4":System.out.println("----取消收藏成功");
                    flagcollect=false;
                    item.setTitle("收藏");
                    break;
                case "3":System.out.println("----取消收藏失败");
                    break;
                case "5":System.out.println("----已收藏");
                    flagcollect=true;
                    item.setTitle("取消收藏");
                    break;
                case "6":System.out.println("----未收藏");
                    flagcollect=false;
                    item.setTitle("收藏");
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    FollowListener listeners=new FollowListener() {
        @Override
        public void onResponse(String msg) {
            //获取菜单视图
            if (item2==null)
                item2=findViewById(R.id.menufocus);

            //根据mode中response返回的字符串区分返回结果
            switch (msg)
            {
                case "2": System.out.println("----关注成功");
                    flagfollow=true;
                    item2.setTitle("取消关注");
                    break;
                case "1":System.out.println("----关注失败");
                    break;
                case "4":System.out.println("----取消关注成功");
                    flagfollow=false;
                    item2.setTitle("关注");
                    break;
                case "3":System.out.println("----取消关注失败");
                    break;
                case "5":System.out.println("----已关注");
                    flagfollow=true;
                    item2.setTitle("取消关注");
                    break;
                case "6":System.out.println("----未关注");
                    flagfollow=false;
                    item2.setTitle("关注");
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_article);
        System.out.println("----查看文章详情");
        context=ViewProjectActivity.this;

        resid  = getIntent().getIntExtra("pid",1);//获取传递的资源id
        idolid=getIntent().getIntExtra("userfocus",1);
        webView = findViewById(R.id.webview);
        webView.loadUrl(Common.PROJECTURL+resid);
        sp=context.getSharedPreferences("login",MODE_PRIVATE);
        readSP();//读取sessionid
    }

    private void readSP() {
        sessionID=sp.getString("sessionID",null);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu. menucollect, menu);//加载菜单布局
        item=menu.findItem(R.id.menucollect);
        item2=menu.findItem(R.id.menufocus);
        collectmodel=new CollectModel();//实例化对象
        followModel=new FollowModel();
        collectmodel.exist("project",resid,sessionID,listener);//判断是否收藏
        followModel.exists("userfocus",idolid,sessionID,listeners);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.menucollect:
                if(flagcollect)//如果已收藏，则调用取消收藏
                {
                    System.out.println("----准备取消收藏");
                    collectmodel.uncollect("project",resid,sessionID,listener);
                }
                else//如果未收藏，则调用收藏
                {
                    System.out.println("----准备收藏");
                    collectmodel.collect("project",resid,sessionID,listener);
                }
                break;
            case R.id.menufocus:
                if(flagfollow)//如果已收藏，则调用取消收藏
                {
                    System.out.println("----准备取消关注");
                    followModel.unfocus("userfocus",idolid,sessionID,listeners);
                }
                else//如果未收藏，则调用收藏
                {
                    System.out.println("----准备关注");
                    followModel.focus("userfocus",idolid,sessionID,listeners);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
